<?php
/**
 * 商户管理
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-04-26 15:54:31
 * @version $Id$
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Bis as BisModel;
use think\Db;

class Bis extends Controller
{
    private $link;

    public function _initialize()
    {
        $this->link = new BisModel();
    }

    public function index()
    {
        $res = $this->link->getBisByStatus(1);
        $this->assign('res',$res);
        return $this->fetch();
    }

    // 入驻申请列表
    public function apply()
    {
        $res = $this->link->getBisByStatus();
        $page = $res->render();
        // dump($res);exit;
        $this->assign('res',$res);
        $this->assign('page',$page);
        return $this->fetch();
    }

    public function detail($id=0)
    {
        if (0 === $id) {
            $this->error('Request Data Error!');
        }
        // 获取一级城市
        $citys = model('city')->getNormalFirstCity();
        // 获取一级栏目
        $categorys = model('category')->getNormalFirstCategory();

        // 获取商户信息
        // 方法一 使用多个model直接获取
        /*$bisData = model('Bis')->get($id);
        $locationData = model('BisLocation')->get(['bis_id'=>$id,'is_main'=>1]);
        $accountData = model('BisAccount')->get(['bis_id'=>$id,'is_main'=>1]);
        return $this->fetch('',[
            'citys'        => $citys,
            'categorys'    => $categorys,
            'bisData'      => $bisData,
            'locationData' => $locationData,
            'accountData'  => $accountData,
        ]);*/

        // 方法二
        $sql = 'SELECT b.name,`username`,b.email,b.logo,`licence_logo`,`description`,b.city_id,
               b.city_path,`bank_user`,`bank_name`,`bank_info`,`faren`,`faren_tel`,b.status,
               b.create_time,`tel`,`contact`,`address`,`open_time`,`content`,`category_id`,
               `category_path` FROM `o2o_bis` AS b LEFT JOIN `o2o_bis_location` AS l ON b.id=l.bis_id
               LEFT JOIN `o2o_bis_account` as a ON b.id=a.bis_id WHERE b.id=? LIMIT 1;';
        $res = Db::query($sql,[$id]);
        // dump($res);exit();
        return $this->fetch('',[
            'citys'        => $citys,
            'categorys'    => $categorys,
            'data'         => $res[0],
        ]);
    }

    public function dellist()
    {
        $res = $this->link->getBisByStatus(-1);
        $this->assign('res',$res);
        return $this->fetch();
    }

    // 状态修改
    public function status(Request $request)
    {
        $data = $request->get();
        $rule = [
            'id'        => 'number',
            'status'    => 'number|in:-1,0,1,2'
        ];
        $validate = $this->validate($data,$rule);// 实例化验证规则
        if(TRUE !== $validate){
            $this->error($validate);
        }
        $res = $this->link->save(['status'=>$data['status']],['id'=>$data['id']]);
        $location = model('BisLocation')->save(['status'=>$data['status']],['bis_id'=>$data['id'],'is_main'=>1]);
        $account = model('BisAccount')->save(['status'=>$data['status']],['bis_id'=>$data['id'],'is_main'=>1]);
        if ($res && $location && $account) {
            // 发送邮件给客户端，说明修改状态 status 1 审核通过 2不通过 -1删除
            /*$url = url('/bis/register/waiting', ['id'=>$bisId], false, 'true');
            $title = 'o2o入驻申请通知';
            $content = '您已提交的入驻申请需要等待平台方审核；您可以通过点击链接<a href="'.$url.' target="_blank">查看连接</a>查看审核状态！';
            if (!$this->sendEmail($data['email'], $title, $content)) {
                $this->error('申请失败！');
            }*/
            $this->success('状态设置成功！');
        }
        $this->error('状态设置失败！');
    }
}
